<?php
namespace app\supplyer\controller\sys_admin;

use app\AdminController;
use app\jxcshop\model\UsersModel;
use app\supplyer\model\SupplyerCategoryModel;
use app\supplyer\model\SupplyerModel;
use app\shop\model\GoodsModel;
/**
 * 供应商帐号管理
 */
class Supplyer extends AdminController
{
	/*------------------------------------------------------ */
	//-- 优先执行
	/*------------------------------------------------------ */
	public function initialize(){
        parent::initialize();
		$this->Model = new SupplyerModel();
    }
	/*------------------------------------------------------ */
    //--首页
    /*------------------------------------------------------ */
    public function index()
    {
		$this->getList(true);

        return $this->fetch('index');
    }

    /**
     * @param $data
     * @return mixed
     */
    public function asInfo($data)
    {
        $model = new SupplyerCategoryModel();
        $category = $model->getRows();
//        $this->assign("category", $category);
//        $ClassList = $this->Model->getClassList();
        $this->assign("classListOpt", arrToSel($category, $data['cid']));//分类
        return $data;
    }
	/*------------------------------------------------------ */
    //-- 获取列表
	//-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {
		$where = [];
        $where[] = ['is_audit','=','1'];
		$keyword = input("keyword");
        if($keyword){
            $where[] = ['company_name','like','%'.$keyword.'%'];
            $where['or'][] = 'supplyer_id = '.$keyword * 1;
        }
        $model = new SupplyerCategoryModel();
        $data = $this->getPageList($this->Model, $where);
        foreach ($data['list'] as &$value){
            $value['cate_name'] = $model->where(['id' => $value['cid']])->value('name');
            $value['images'] = explode(',',$value['images']);
        }
		$this->assign("data", $data);
		if ($runData == false){
			$data['content']= $this->fetch('list')->getContent();
			unset($data['list']);
			return $this->success('','',$data);
		}
        return true;
    }

	/*------------------------------------------------------ */
	//-- 添加前处理
	/*------------------------------------------------------ */
    public function beforeAdd($data) {
        if (empty($data['supplyer_name'])){
            return $this->error('请输入供应商名称.');
        }
		$count = $this->Model->where('supplyer_name',$data['supplyer_name'])->count('supplyer_id');
		if ($count > 0) return $this->error('操作失败:已存在供应商名称，不允许重复添加！');
		$data['add_time'] = time();
		$data['update_time'] = time();
		$data['password'] = _hash($data['password']);
		return $data;
	}
    /*------------------------------------------------------ */
    //-- 添加后调用
    /*------------------------------------------------------ */
    public function afterAdd($data)
    {
        $logInfo = '添加供应商帐号，供应商帐号状态：';
        $logInfo .= $data['is_ban'] == 1 ? '封禁':'正常';
        $this->_Log($data['supplyer_id'],$logInfo);
        return $this->success('修改成功.',url('index'));
    }
	/*------------------------------------------------------ */
	//-- 修改前处理
	/*------------------------------------------------------ */
    public function beforeEdit($data){
        if (empty($data['supplyer_name'])){
            return $this->error('请输入供应商名称.');
        }
        $data['is_ban'] = $data['is_ban'] * 1;
        $where[] = ['supplyer_name','=',$data['supplyer_name']];
        $where[] = ['supplyer_id','<>',$data['supplyer_id']];
        $count = $this->Model->where($where)->count('supplyer_id');
        if ($count > 0) return $this->error('操作失败:已存在供应商名称，不允许重复添加！');
		$data['update_time'] = time();
		if (empty($data['password']) == false){
			$data['password'] = _hash($data['password']);
		}else{
		    unset($data['password']);
        }
        $logInfo = '修改供应商信息，状态：'.($data['is_ban'] == 1 ? '封禁':'正常');
        $this->_log($data['supplyer_id'], $logInfo ,'supplyer');
		return $data;
	}
    /*------------------------------------------------------ */
    //-- 修改后调用
    /*------------------------------------------------------ */
    public function afterEdit($data){
        $logInfo = '供应商帐号状态：';
        if ($data['is_ban'] == 1){
            $logInfo .= '封禁';
            //批量执行商品下架
            (new GoodsModel)->where('supplyer_id',$data['supplyer_id'])->update(['isputaway'=>0]);
        }else{
            $logInfo .= '正常';
        }
        if (empty($data['password']) == false){
            $logInfo .= '，修改供应商密码.';
            $this->_Log($data['supplyer_id'],'平台修改供应商密码','supplyer');
        }
        $this->_Log($data['supplyer_id'],$logInfo);
        return $this->success('修改成功.',url('index'));
    }

    /*------------------------------------------------------ */
    //--首页
    /*------------------------------------------------------ */
    public function audit()
    {
        $this->getAuditList(true);

        return $this->fetch('audit');
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getAuditList($runData = false) {
        $where = [];
        $where[] = ['is_audit','in','0,2'];
        $keyword = input("keyword");
        if($keyword){
            $where[] = ['company_name','like','%'.$keyword.'%'];
            $where['or'][] = 'supplyer_id = '.$keyword * 1;
        }
        $model = new SupplyerCategoryModel();
        $userModel = new UsersModel();
        $data = $this->getPageList($this->Model, $where);
        foreach ($data['list'] as &$value){
            $value['cate_name'] = $model->where(['id' => $value['cid']])->value('name');
            $value['user_name'] = $userModel->where(['user_id' => $value['uid']])->value('nick_name');
            $value['images'] = explode(',',$value['images']);
        }
        $this->assign("data", $data);
        if ($runData == false){
            $data['content']= $this->fetch('audit_list')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return true;
    }

    public function opAudit()
    {
        $supplyer_id= input('supplyer_id', 0, 'intval');
        $row = $this->Model->where('supplyer_id',$supplyer_id)->find();
        if ($this->request->isPost()) {
            $is_audit = input('is_audit', '');
            $refuse_reason = input('refuse_reason', '');
            $res = $this->Model->where('supplyer_id',$supplyer_id)->update([
                'is_audit' => $is_audit,
                'refuse_reason' => $refuse_reason
            ]);
            if ($res < 1) return $this->error('操作失败,请重试.');
            return $this->success('审核成功！', 'reload');
        }
        $this->assign("row", $row);
        return $this->fetch('op_audit');

    }
}
